<?php

namespace Pw\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Pw\MainBundle\Form\Type\HeroType;
use Pw\MainBundle\Form\Type\HeroEditType;
use Pw\MainBundle\Entity\Hero;
use Pw\MainBundle\Classes\NFAParse;
class HeroController extends Controller
{
	
    public function testAction(){
		$em = $this->getDoctrine()->getManager();
		$router = $this->get('router');
		$request = $this->getRequest();
		//$heroes = $em->getRepository('PwMainBundle:Hero')->findAll();
		$parser = new NFAParse($em, $request, $router);

		//$raw = 'яч[b param1=value1,param2=value2]см[/b] [hero=Мастер клинков, type=icon]<input type="text">[video source=http://www.youtube.com/embed/4hpEnLtqUDg]color=red,tro=lolo][color=faa]dad[talent=2, type=icon]asdas[/color][gb=lol]';
		//$raw = '[video source=http://www.youtube.com/embed/JK8zVhqD_V0]';
		$parsed = '';
		if ($request->isMethod('POST')) {
			$input = $request->get('inputarea');
			if ($input) {
				$parsed = $parser->parse($input);
			}
			//$request->get($raw)
		}
		return $this->render('PwMainBundle:Hero:test.html.php',  array('parsed' => $parsed));
    }
    public function indexAction(){
		$em = $this->getDoctrine()->getManager();
		$heroes = $em->getRepository('PwMainBundle:Hero')->findAll();
		return $this->render('PwMainBundle:Hero:index.html.php',  array('heroes' => $heroes));
    }
	public function addAction(){
		$em = $this->getDoctrine()->getManager();
		$hero = new Hero();
		$form = $this->createForm(new HeroType(), $hero, array('em' => $em));
		$request = $this->getRequest();
		if ($request->isMethod('POST')) {
			$form->bind($request);
			if ($form->isValid()) {				
				$hero->doUpload();
				$em->persist($hero);
				$em->flush();
				return $this->redirect($this->generateUrl('heroes_show'));
			}
		}
		return $this->render('PwMainBundle:Hero:add.html.php',  array('form' => $form->createView()));		
	}

	public function editAction($heroId){
		$em = $this->getDoctrine()->getEntityManager();
		$hero = $em->getRepository('PwMainBundle:Hero')->find($heroId);
		if ($hero) {
			$form = $this->createForm(new HeroEditType(), $hero, array('em' => $em));
			$request = $this->getRequest();		
			if ($request->isMethod('POST')) {
				$form->bind($request);
				if ($form->isValid()) {
					$hero->doUpload();
					$em->flush();
					return $this->redirect($this->generateUrl('heroes_show'));
				}
			}
			return $this->render('PwMainBundle:Hero:edit.html.php',  array('form' => $form->createView(), 'heroId' => $heroId));
		}
		throw $this->createNotFoundException();
	}
	public function detailsAction($heroName){
		$em = $this->getDoctrine()->getEntityManager();
		$hero = $em->getRepository('PwMainBundle:Hero')->findByName($heroName);
		if ($hero) {

			return $this->render('PwMainBundle:Hero:details.html.php',  array('hero' => $hero));
		}
		throw $this->createNotFoundException();
	}
	public function deleteAction($heroId){
		$em = $this->getDoctrine()->getEntityManager();
		$hero = $em->getRepository('PwMainBundle:Hero')->find($heroId);
		if ($hero) {
			$em->remove($hero);
			$em->flush();			
		}
		return $this->redirect($this->generateUrl('heroes_show'));
	}
	
	public function getJsonAction($heroId){
		$em = $this->getDoctrine()->getEntityManager();
                $result = array();
                if ($heroId) {
                    $hero = $em->getRepository('PwMainBundle:Hero')->find($heroId);		
                    if ($hero) {
                        $result = $hero->getArray4JSON();
                    }
                } else {
                    $heroes = $em->getRepository('PwMainBundle:Hero')->findVisible();
                    foreach ($heroes as $h) {
                        $result[$h->getId()] = $h->getArray4JSON();
                    }
                }

		return new JsonResponse($result);
	}
}
